package com.enuo.filter;

import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import com.enuo.utils.AjaxMsg;
import com.enuo.utils.WebUtils;

/**
 * @ClassName: AuthenticationFilter 
 * @Description: TODO(权限认证) 
 * @author Charles 
 * @date 2017年11月16日 下午2:18:48 
 * @version 1.0
 */
public class AuthenticationFilter extends FormAuthenticationFilter
{

    @Override
    protected AuthenticationToken createToken(ServletRequest servletRequest,
            ServletResponse servletResponse)
    {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String username = getUsername(servletRequest);
        String password = getPassword(servletRequest);
        boolean rememberMe = isRememberMe(servletRequest);
        
        return new UsernamePasswordToken(username, password, rememberMe, WebUtils.getRemoteIP(request));
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception
    {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        return super.onAccessDenied(request, response);
    }

    /**
     * 如果注册成功的处理
     */
    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest servletRequest, ServletResponse servletResponse)
        throws Exception
    {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        Session session = subject.getSession();
        Map<Object, Object> attributes = new HashMap<Object, Object>();
        Collection<Object> keys = session.getAttributeKeys();
        for (Object key : keys)
        {
            attributes.put(key, session.getAttribute(key));
        }
        session.stop();
        session = subject.getSession();
        for (Entry<Object, Object> entry : attributes.entrySet())
        {
            session.setAttribute(entry.getKey(), entry.getValue());
        }

        // for AJAX request
        if (WebUtils.isAjaxRequest(request))
        {
            // to return json protocol string
            servletResponse.setCharacterEncoding("UTF-8");
            PrintWriter out = servletResponse.getWriter();
            out.println(AjaxMsg.success());
            out.flush();
            out.close();
            return false;
        }
        else
        {
            return super.onLoginSuccess(token, subject, servletRequest, servletResponse);
        }
    }

    @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception
    {
        return super.executeLogin(request, response);
    }
}